package Ventanas;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import ClasesBasicas.Cliente;
import ClasesBasicas.Ingrediente;
import ClasesBasicas.PizzaSugerida;
import Gestores.SistemaPizzaSugerida;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.Color;

public class ventanaVotos extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3024559798365593938L;
	private JPanel contentPane;
	private JTextField ing1textField;
	private JTextField ing2textField;
	private JTextField ing3textField;
	private JComboBox comboBox;
	private JTextField salsatextField;
	private Cliente c = new Cliente();
	private JLabel label;

	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ventanaVotos frame = new ventanaVotos();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public ventanaVotos() throws ClassNotFoundException, SQLException {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage("src\\Fotos\\txoPizzeria.jpg"));
		setTitle("Votos");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 510, 384);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		comboBox = new JComboBox();
		
		comboBox.setBounds(195, 32, 134, 20);
		contentPane.add(comboBox);
		SistemaPizzaSugerida sp=new SistemaPizzaSugerida();
		ArrayList<String> sugeridas=sp.usersList();
		int i=0;
		if(!sugeridas.isEmpty()){
			while(i<sugeridas.size()){
				comboBox.addItem(sugeridas.get(i));
				i++;
			}
		}
		
		
		JLabel lblNewLabel = new JLabel("Ingrediente 1");
		lblNewLabel.setBounds(36, 95, 98, 20);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Ingrediente 2");
		lblNewLabel_1.setBounds(36, 149, 91, 14);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Ingrediente 3");
		lblNewLabel_2.setBounds(36, 197, 74, 14);
		contentPane.add(lblNewLabel_2);
		
		ing1textField = new JTextField();
		ing1textField.setEditable(false);
		ing1textField.setBounds(157, 95, 134, 20);
		contentPane.add(ing1textField);
		ing1textField.setColumns(10);
		
		
		ing2textField = new JTextField();
		ing2textField.setEditable(false);
		ing2textField.setBounds(157, 144, 134, 20);
		contentPane.add(ing2textField);
		ing2textField.setColumns(10);
		
		ing3textField = new JTextField();
		ing3textField.setEditable(false);
		ing3textField.setBounds(158, 191, 133, 20);
		contentPane.add(ing3textField);
		ing3textField.setColumns(10);
		
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(comboBox.getSelectedItem()!=null){
					SistemaPizzaSugerida sp=new SistemaPizzaSugerida();
					PizzaSugerida p;
					try {
						p = sp.buscarPizza(comboBox.getSelectedItem().toString());
						Ingrediente [] ing=p.getaIngredientes();
						String ing1=ing[0].getIngrediente();
						ing1textField.setText(ing[0].getIngrediente());
						ing2textField.setText(ing[1].getIngrediente());
						ing3textField.setText(ing[2].getIngrediente());
						salsatextField.setText(p.getSalsa());
						
						
						
					} catch (ClassNotFoundException | SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
					
				}
			}
		});
		
		JButton btnVotar = new JButton("Votar");
		btnVotar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					SistemaPizzaSugerida.conectar();
					SistemaPizzaSugerida.actualizarPuntos(comboBox.getSelectedItem().toString());
					SistemaPizzaSugerida.desconectar();
					JOptionPane.showMessageDialog(null, "Su voto a la pizza "+ comboBox.getSelectedItem().toString()+ " se ha registrado correctamente");
					setVisible(false);
					ventanaClienteLogeado v=new ventanaClienteLogeado();
					v.setCliente(c);
					v.setLocationRelativeTo(null);
					v.setVisible(true);
				} catch (ClassNotFoundException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnVotar.setBounds(138, 311, 89, 23);
		contentPane.add(btnVotar);
		
		JButton btnVolver = new JButton("Volver");
		btnVolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				ventanaClienteLogeado v=new ventanaClienteLogeado();
				v.setCliente(c);
				v.setLocationRelativeTo(null);
				v.setVisible(true);
			}
		});
		btnVolver.setBounds(10, 11, 74, 23);
		contentPane.add(btnVolver);
		
		JLabel lblSalsa = new JLabel("Salsa");
		lblSalsa.setBounds(38, 238, 46, 14);
		contentPane.add(lblSalsa);
		
		salsatextField = new JTextField();
		salsatextField.setEditable(false);
		salsatextField.setBounds(157, 235, 134, 20);
		contentPane.add(salsatextField);
		salsatextField.setColumns(10);
		
		label = new JLabel("");
		label.setIcon(new ImageIcon("src\\Fotos\\images (4).jpg"));
		label.setBounds(301, 62, 193, 283);
		contentPane.add(label);
	}
	public Cliente getC() {
		return c;
	}

	public void setC(Cliente c) {
		this.c = c;
	}
}
